package bizapi.sinosoft.com.config;

import com.weibo.api.motan.config.springsupport.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import utils.Lang;

/**
 * Created by xujingfeng on 2017/2/15.
 */
@Configuration
public class MotanConfig {

    @Value("${motan.client.group}")
    private String motanClientGroup;

    @Value("${motan.client.access.log}")
    private Boolean motanClientAccessLog;

    @Value("${motan.server.group}")
    private String motanServerGroup;

    @Value("${motan.server.access.log}")
    private Boolean motanServerAccessLog;

    @Bean
    public AnnotationBean motanAnnotationBean() {
        AnnotationBean motanAnnotationBean = new AnnotationBean();
        motanAnnotationBean.setPackage("utils.rpc.motan,bizapi.sinosoft.com");
        if(motanAnnotationBean.getPackage()==null){
            throw new RuntimeException("请配置maton api 包");
        }
        return motanAnnotationBean;
    }

    @Bean(name = "motan")
    public ProtocolConfigBean protocolConfig() {
        ProtocolConfigBean config = new ProtocolConfigBean();
        config.setDefault(true);
        config.setSerialization("hibernateHessian2");
        //config.setSerialization("fastjson");
        config.setName("motan");
        config.setMaxContentLength(1048576);
        config.setRequestTimeout(50000000);
        return config;
    }

    @Bean(name = "registry")
    public RegistryConfigBean registryConfigSit(@Value("${motan.zookeeper.host}")String zookeeperAddress) {
        RegistryConfigBean config = new RegistryConfigBean();
        config.setDefault(true);
        config.setRegProtocol("zookeeper");
        config.setAddress(zookeeperAddress);
        return config;
    }

    /**
     * 基础服务端配置
     * @param port
     * @return
     */
    @Bean(name = "motanServerBasicConfig")
    public BasicServiceConfigBean baseServiceConfig(@Value("${motan.export.port}") String port) {
        BasicServiceConfigBean config = new BasicServiceConfigBean();
        config.setDefault(true);
        config.setExport("motan:"+port);
//        config.setGroup("sinomall");
//        config.setAccessLog(false);
        if(Lang.isEmpty(motanServerGroup)){
            config.setGroup("sinomall");
        }else {
            config.setGroup(motanServerGroup);
        }
        if(Lang.isEmpty(motanServerAccessLog)){
            config.setAccessLog(true);
        }else {
            config.setAccessLog(motanServerAccessLog);
        }
        config.setShareChannel(true);
        //config.setModule("motan-demo-rpc");
        //config.setApplication("myMotanDemo");
        config.setRegistry("registry");
        config.setFilter("openjpasession");
        config.setRequestTimeout(900000);
        return config;
    }


    @Bean(name = "motanClientBasicConfig")
    public BasicRefererConfigBean baseRefererConfig() {
        BasicRefererConfigBean config = new BasicRefererConfigBean();
        config.setProtocol("motan");
       // config.setGroup("sinomall");
        if(Lang.isEmpty(motanClientGroup)){
            config.setGroup("sinomall");
        }else {
            config.setGroup(motanClientGroup);
        }
        if(Lang.isEmpty(motanClientAccessLog)){
            config.setAccessLog(true);
        }else {
            config.setAccessLog(motanClientAccessLog);
        }
        ///config.setModule("motan-demo-rpc");
        //config.setApplication("myMotanDemo");
        config.setRegistry("registry");
        config.setCheck(false);
       // config.setAccessLog(false);
        config.setRetries(2);
        config.setThrowException(true);
        config.setDefault(true);
        config.setRequestTimeout(5000);
        return config;
    }
}
